var json = [{
    'id': 1,
    "src": "//img14.360buyimg.com/n7/jfs/t1/78442/37/5440/135812/5d3824a3E03c4f481/f4b6f9f703a64d7e.jpg",
    "price": 1099,
    "name": "李宁闪击篮球鞋驭帅10镭射队尚4男韦德之道空袭中高帮队尚3.5球鞋",
    "sale": "45"
  },
  {
    'id': 2,
    "src": "//img10.360buyimg.com/n7/jfs/t1/61083/5/2564/266150/5d0da810E1f5798c6/2e909cfa6532460e.jpg",
    "price": 99.00,
    "name": "Sport飓风 Nike Kwazi 休闲运动鞋男 844839-002-001-100-400",
    "sale": 69
  },
  {
    'id': 3,
    "src": "https://goods1.juancdn.com/goods/200606/1/2/5edb55dfb6f8ea730f0ef0d9_800x800.jpg?iopcmd=thumbnail&type=11&height=310&width=310%7Ciopcmd=convert&Q=88&dst=jpg",
    "price": 158.00,
    "name": "华为 Mate 9 Pro 6GB+128GB版 银钻灰 移动联通电信4G手机 双卡双待",
    "sale": 78
  },
  {
    'id': 4,
    "src": "//img10.360buyimg.com/n7/jfs/t1/20234/33/6773/107919/5c6375ddE8b932351/b2e45ffe115de182.jpg",
    "price": 1099.00,
    "name": "指南针运动 NIKE HYPERSHIFT篮球鞋 844392-010-607-164-017现货",
    "sale": 92
  }, {
    'id': 5,
    "src": "//img11.360buyimg.com/n7/jfs/t1/70735/2/10504/351449/5d7eeb6fEb1300919/ca8c50e53fffdf51.jpg",
    "price": 159.00,
    "name": "【莹恋】MIZUNO美津浓V1GA159002乒乓球鞋男鞋女鞋室内综合训练鞋",
    "sale": 59
  }, {
    'id': 6,
    "src": "//img12.360buyimg.com/n7/jfs/t1/65202/33/7698/74842/5d5b43d6E89104837/51dc9302b388266a.jpg",
    "price": 299.00,
    "name": "安踏女鞋运动鞋女2019秋冬季户外休闲男鞋老爹鞋学生粉色圆头旅行鞋轻便情侣小",
    "sale": 92
  
  }
    , {
    'id': 7,
    "src": "//img14.360buyimg.com/n7/jfs/t1/82697/6/9918/184964/5d778ee4E1427ec30/b098ba4aaa58c60b.jpg",
    "price": 300.00,
    "name": "回力老爹鞋女学生秋季透气百搭小白鞋2019新款白色ins潮流运动休闲鞋女鞋子",
    "sale": 80
  }, {
    'id': 8,
    "src": "//img11.360buyimg.com/n7/jfs/t1/70735/2/10504/351449/5d7eeb6fEb1300919/ca8c50e53fffdf51.jpg",
    "price": 1099.00,
    "name": "安踏女鞋运动鞋女2019秋冬季户外休闲男鞋老爹鞋学生粉色圆头旅行鞋轻便情侣小",
    "sale": 87
  }
  ];
 
  function getGoods (data) {
    var tmpData = data || json;
    let str = '';
    tmpData.forEach(ele => { 
      let { id, src, price, name, sale } = ele;
      str += `<div class="goodsCon"><a target = "_blank" >
        <img src="${src}" class="icon"><h4 class="title">${name}</h4>
        <div class="info">限时抢购200条</div></a><div class="priceCon">
        <span class="price">￥${price}</span>
        <span class="oldPrice">￥${(price * 1.2).toFixed(2)}</span>
        <div><span class="soldText">已售${sale}%</span>
        </span></span></div>
        <a class="button" target="_blank" onclick="addCart(${id},'${name}','${src}','${price}',1)">请不要点我</a></div></div >`;
    });
    document.querySelector('.divs').innerHTML += str;
  }
  getGoods();
  window.onscroll = function () {
    var ct = document.querySelectorAll('.goodsCon')[document.querySelectorAll('.goodsCon').length - 1].offsetTop;
    var st = getScTop();
    var clintTop = getClient().h;
    if (ct < (st + clintTop)) {
    
      get('../php/server.php', function (res) {
        getGoods(JSON.parse(res))
        console.log(1)
      })
    }
  }
  function getClient () {
    return {
      h: window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight,
      w: window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth,
    }
  }
  function getScTop () {
    return window.pageYOffset || document.body.scrollTop;
  }
  function get (url, cb) {
    let xhr = new XMLHttpRequest();
    xhr.open('get', url);
    xhr.send();
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        cb && cb(xhr.response)
      }
    }
  
  }
 